<!--
 * @Author: 陈俊升 761723995@qq.com
 * @Date: 2023-12-12 11:14:35
 * @LastEditors: 陈俊升 761723995@qq.com
 * @LastEditTime: 2024-01-02 15:13:38
 * @FilePath: /hr_new_vue3/src/views/Management/Home/LeaveManagement/Certificate/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!-- 首页-劳动合同-我的合同 -->
<template>
  <ContentCard>
    <template #header>
      <PageHeaderNew title="我的合同" />
    </template>

    <TableTurn :tableData="getTableData" ref="tableRef">
      <a-table-column :width="80">
        <template #title> 序号 </template>
        <template #cell="{ rowIndex }">
          {{ rowIndex + 1 }}
        </template>
      </a-table-column>
      <a-table-column
        :width="150"
        title="合同名称"
        data-index="contract.contractName"
      />
      <a-table-column
        :width="150"
        title="合同编号"
        data-index="contract.contractNum"
      />
      <a-table-column :width="150" title="合同类型">
        <template #cell="{ record }">
          {{ findType(record.contract.typeId) }}
        </template>
      </a-table-column>
      <a-table-column
        :width="150"
        title="合同公司"
        data-index="contract.companyId"
      >
        <template #cell="{ record }">
          {{
            companyList.find((item) => item.id === record.contract.companyId)
              ?.name
          }}
        </template>
      </a-table-column>
      <!-- <a-table-column :width="150" title="发起人" data-index="userName">
      </a-table-column> -->
      <a-table-column
        :width="150"
        title="发起签署时间"
        data-index="contract.startTime"
      />
      <a-table-column :width="150" title="状态" data-index="status">
        <template #cell="{ record }">
          <div
            :style="{
              color: contractUserStatus.find(
                ({ value }) => value === record.contract.status
              )?.color
            }"
          >
            {{
              contractUserStatus.find(
                ({ value }) => value === record.contract.status
              )?.text || '-'
            }}
          </div>
        </template>
      </a-table-column>
      <a-table-column :width="150" title="操作" data-index="">
        <template #cell="{ record }">
          <!-- <a-button
              type="text"
              v-if="record.handleStatus === '已完成'"
              @click="handleClick(record, 2)"
              >查看</a-button
            > -->
          <!-- <a-popconfirm content="确认处理该条数据吗?" @ok="handleClick(record)">
            <a-button type="text">处理</a-button>
          </a-popconfirm> -->
          <a-button
            type="text"
            @click="handle(record, 1)"
            v-if="record.contractStatus === 3"
          >
            处理
          </a-button>
          <a-button type="text" @click="handle(record, 2)"> 查看 </a-button>
        </template>
      </a-table-column>
    </TableTurn>
  </ContentCard>
</template>

<script lang="ts" setup>
import { GetLegalEntityList } from '@/apis/management/organization/organizationManagement/LegalEntity/index';
import { GetContractGroup } from '@/apis/management/contract/baseSetting';
import { contractUserStatus } from '@/utils/dictionary/contract';
import { GetMyContractPage } from '@/apis/management/contract/contractManagement/haveSigned';

const router = useRouter();

let findType = (typeId: any) => {
  console.log(typeId);
  let name = '-';
  typeList.value.every(({ children }) => {
    let find = children?.find((item: any) => item.id === typeId);
    if (find) {
      name = find.name;
      return false;
    }
    return true;
  });
  return name;
};
const getTableData = async (pageData?: any) => {
  try {
    let res = await GetMyContractPage({
      ...pageData,
      id: localStorage.getItem('userId')
    });
    // res.content = res.content.map((el: any) => {
    //   el.handleStatus = handleTableStatus(el?.children);
    //   el.details = cloneDeep(el?.children);
    //   el.handlerMatters = handleMatters(el?.details);
    //   delete el.children;
    //   return el;
    // });
    return Promise.resolve(res);
  } catch (error: unknown) {
    console.log(error);
  }
};

// 处理办理事项
const handleMatters = (arr: []) => {
  let temp: any = [];
  arr.forEach((item: any) => {
    if (item.handlerUser === 153) {
      temp.push(item.handlerMatters);
    }
  });
  return temp.join(';');
};

// 处理表格数据的状态
const handleTableStatus = (arr: []) => {
  let unFinishedCount = 0; // 未完成的数量
  let finishedCount = 0; // 已完成的数量
  let totalCount = 0; // 自己所需办理事件的总数
  arr.forEach((el: any) => {
    if (el.handlerUser === 153) {
      totalCount++;
      if (el.handlerEnter === 1) {
        unFinishedCount++;
      } else if (el.handlerEnter === 2) {
        finishedCount++;
      }
    }
  });
  if (unFinishedCount === totalCount) {
    return '待确认';
  } else if (finishedCount === totalCount) {
    return '已完成';
  } else return '处理中';
};

// 处理或者查看详情 type 1处理 2查看
const handle = ({ contract, contractStatus }: any, type: 1 | 2) => {
  localStorage.setItem(
    'myContractData',
    JSON.stringify({ ...contract, contractStatus })
  );
  router.push({
    name: 'MyHandleContract'
  });
};

let typeList = ref<any[]>([]);
let companyList = ref<any[]>([]);
let loadData = async () => {
  let [contractType, legalEntity] = await Promise.all([
    GetContractGroup({
      pageNumber: 0,
      pageSize: -1
    }),
    GetLegalEntityList({ pageNumber: 0, pageSize: -1 } as any)
  ]);
  typeList.value = contractType?.content || [];
  companyList.value = legalEntity?.content || [];
};
loadData();
</script>

<style lang="less" scoped></style>
